package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
/**
 * 
 * @author msjykf
 * 邮件、短信、对账单批次处理
    select type,batch_id,template_id
    from
    (
    select acceptstate||1 as type,batch_id,groupid as template_id from customer_group_synchis where acceptstate=1 and createdate>trunc(sysdate) and procstate=0
    union all
    select state||2||procstate as type,batch_id,template_id from email_send_batch where state in(-1,1) and procstate in(0,1) and createdate>trunc(sysdate)
    union all 
    select state||3 as type,batch_id,sms_id as template_id from sms_send_batch where state in(-1,1) and createdate>trunc(sysdate) and rownum<=5
    union all 
    select acceptstate||4 as type,batch_id,template_id from statement_batch where acceptstate in(-1,1) and createdate>trunc(sysdate)
    ) 
 *
 */
public class BatchSchedTask implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(BatchSchedTask.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		PropertiesUtil properties = new PropertiesUtil();
		def ccserverip = properties.readValue("ccserverip"); //获取CallCenter访问地址
		def type = map.get("type"); //类型 
		def batch_id = map.get("batch_id"); //批次号
		def template_id = map.get("template_id"); //模板ID
		def charset = "utf-8"; //字符集
		def timeout = 100000; //请求超时时间
		def enableSSLflag = 0;
		def interfacename = "";
		def process = 0;
		def smap = "";
		def c_taskcode = "";
		switch(type) {
			case '11':
			def querysql = "{call sp_customergroup_deal(?,?,?,?)}";
			def input = new String[3];
			input[0] = batch_id;
			input[1] = template_id;
			input[2] = 1;
			log.debug("input:"+input);
			eliteBean.executeWithReturn(querysql,input,"dataSource");
			process = 1;
			break;
			case '-120':
			interfacename = "EmailBatchRequest";
			break;
			case '120':
			//邮件黑名单过滤
			smap = eliteBean.chaxun_sql("select c_taskcode,saletype from doctemplate where doctemplate_id='"+template_id+"'","dataSource");
			c_taskcode = smap[0].get("c_taskcode","9999"); //模板ID
			def saletype = smap[0].get("saletype",""); //销售类型
			def exclusions = ""; //邮件黑名单过滤条件
			if(saletype) {
				exclusions = "select sk_invpty from specialphone where blacktype='EMAIL' and status=1 and sk_invpty is not null and saletype in(0,"+saletype+")";
			}else {
				exclusions = "select sk_invpty from specialphone where blacktype='EMAIL' and status=1 and sk_invpty is not null and saletype=0";
				
			};
			if(c_taskcode.equals("0001") || c_taskcode.equals("0005")) {
				String[] data=new String[2];
				data[0] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[1] = "update email_send_batch set procstate=1 where batch_id='"+batch_id+"'";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(c_taskcode.equals("0002")) {
				String[] data=new String[3];
				data[0] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[1] = "delete from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[2] = "update email_send_batch set procstate=1 where batch_id='"+batch_id+"'";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(c_taskcode.equals("0003")) {
				String[] data=new String[3];
				data[0] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[1] = "delete from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[2] = "update email_send_batch set procstate=1 where batch_id='"+batch_id+"'";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(c_taskcode.equals("0004")) {
				String[] data=new String[3];
				data[0] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[1] = "delete from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[2] = "update email_send_batch set procstate=1 where batch_id='"+batch_id+"'";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(c_taskcode.equals("0006")) {
				String[] data=new String[5];
				data[0] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[1] = "delete from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[2] = "delete from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[3] = "delete from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
				data[4] = "update email_send_batch set procstate=1 where batch_id='"+batch_id+"'";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			};
			process = 1;
			break;
			case '121':
			smap = eliteBean.chaxun_sql("select c_taskcode from doctemplate where doctemplate_id='"+template_id+"'","dataSource");
			c_taskcode = smap[0].get("c_taskcode","9999"); //模板ID
			smap = eliteBean.chaxun_sql("select count(*) as procstate from email_custinfo where batch_id='"+batch_id+"'","dataSource");
			int procstate = smap[0].get("procstate");
			if(procstate==0) {
				eliteBean.update("update email_send_batch set state=3,procstate=2 where batch_id='"+batch_id+"'","dataSource");
				if(c_taskcode.equals("0001")) {
					def birthdaysmsid = properties.readValue("birthdaysmsid"); //生日祝福短信模板ID
					eliteBean.update("update sms_send_batch set state=1,acceptdate=sysdate,comments='执行成功' where sms_id='"+birthdaysmsid+"' and state=0 and createdate>trunc(sysdate)","dataSource");
				}
			};
			process = 1;
			break;
			case '-13':
			interfacename = "SmsBatchRequest";
			break;
			case '13':
			interfacename = "SmsCreate";
			break;
			case '-14':
			interfacename = "BillBatchRequest";
			break;
			case '14':
			smap = eliteBean.chaxun_sql("select count(*) as findrow from statement_custinfo where procstate=0 and batch_id='"+batch_id+"'","dataSource");
			int findrow = smap[0].get("findrow");
			if(findrow==0) {
				eliteBean.update("update statement_batch set acceptstate=3 where batch_id='"+batch_id+"'","dataSource");
			};
			process = 1;
			break;
		};
		if(process == 0) {
			def url = "http://"+ccserverip+"/EliteGeneralHttpTask/GeneralService/"+interfacename;
			def jsonbody = "{\"batch_id\":\""+batch_id+"\",\"template_id\":\""+template_id+"\",\"requesttype\": 1}";
			Util.doHttpPost(url,jsonbody,charset,timeout,enableSSLflag);
		}	
	}
}